// ----------------------------------------------------------------------------------
// Microsoft Developer & Platform Evangelism
// 
// Copyright (c) Microsoft Corporation. All rights reserved.
// 
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
// ----------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
// ----------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.WindowsAPICodePack.Taskbar;

namespace TaskbarConcepts
{
    /// <summary>
    /// Interaction logic for Thumbnail.xaml
    /// </summary>
    public partial class Thumbnail : UserControl
    {
        public Thumbnail()
        {
            InitializeComponent();
            this.Unloaded += Thumbnail_Unloaded;
        }

        private void Thumbnail_Unloaded(object sender, RoutedEventArgs e)
        {
            TabbedThumbnail preview = TaskbarManager.Instance.TabbedThumbnail.GetThumbnailPreview(image);
            if (preview != null)
                TaskbarManager.Instance.TabbedThumbnail.RemoveThumbnailPreview(preview);

            preview = TaskbarManager.Instance.TabbedThumbnail.GetThumbnailPreview(trickyBorder);
            if (preview != null)
                TaskbarManager.Instance.TabbedThumbnail.RemoveThumbnailPreview(preview);
        }
        
        private BitmapImage _sneakySource;

        //The following method is part of the lab. When a thumbnail is requested, provide it on demand
        //by loading an image from a resource.
        private void TabbedThumbnail_TabbedThumbnailBitmapRequested(object sender, TabbedThumbnailEventArgs e)
        {
            if (_sneakySource == null)
            {
                _sneakySource = new BitmapImage();
                _sneakySource.BeginInit();
                _sneakySource.DecodePixelHeight = 200;
                _sneakySource.UriSource = new Uri("pack://application:,,,/assets/SpeedyGonzales.jpg");
                _sneakySource.EndInit();
            }
            e.TabbedThumbnail.SetImage(_sneakySource);
        }

        //The following method is part of the lab. When asked to create a thumbnail, check whether
        //the thumbnail already exists and if it doesn't, calculate the offset of the image from the
        //main frame and create a new TabbedThumbnail with the image and the offset.
        private void createThumbnail_Click(object sender, RoutedEventArgs e)
        {
            if (!Utilities.HasThumbnailPreview(image))
            {
                Vector offset = Utilities.GetOffset(Application.Current.MainWindow, image);
                TabbedThumbnail preview = new TabbedThumbnail(Application.Current.MainWindow, image, offset);
                TaskbarManager.Instance.TabbedThumbnail.AddThumbnailPreview(preview);
                preview.Tooltip = "This image will be replaced and invalidated";
                TaskbarManager.Instance.TabbedThumbnail.SetActiveTab(preview);
            }
        }

        //The following method is part of the lab. When asked to remove the thumbnail, make
        //sure it exists first and then remove it.
        private void removeThumbnail_Click(object sender, RoutedEventArgs e)
        {
            TabbedThumbnail preview = TaskbarManager.Instance.TabbedThumbnail.GetThumbnailPreview(image);
            if (preview != null)
                TaskbarManager.Instance.TabbedThumbnail.RemoveThumbnailPreview(preview);
        }

        private void updateImage_Click(object sender, RoutedEventArgs e)
        {
            image.Source = new BitmapImage(new Uri("pack://application:,,,/assets/SmileyTongue.png"));
        }

        //The following method is part of the lab. Get the preview and invalidate the thumbnail
        //directly if it exists.
        private void invalidateThumbnail_Click(object sender, RoutedEventArgs e)
        {
            TabbedThumbnail preview = TaskbarManager.Instance.TabbedThumbnail.GetThumbnailPreview(image);
            if (preview != null)
                preview.InvalidatePreview();
        }

        //The following method is part of the lab. Create a thumbnail and register for the event so that
        //you can render its bitmap on demand. Use an offset from the main window to the black image.
        private void createOnDemandThumbnail_Click(object sender, RoutedEventArgs e)
        {
            if (!Utilities.HasThumbnailPreview(trickyBorder))
            {
                Vector offset = Utilities.GetOffset(Application.Current.MainWindow, trickyBorder);
                TabbedThumbnail preview = new TabbedThumbnail(Application.Current.MainWindow, trickyBorder, offset);
                TaskbarManager.Instance.TabbedThumbnail.AddThumbnailPreview(preview);

                preview.TabbedThumbnailBitmapRequested += TabbedThumbnail_TabbedThumbnailBitmapRequested;

                preview.Tooltip = "This bitmap is created and returned on demand";
            }
        }

        //The following method is part of the lab. Get the thumbnail and if it exists, remove it.
        private void removeOnDemandThumbnail_Click(object sender, RoutedEventArgs e)
        {
            TabbedThumbnail preview = TaskbarManager.Instance.TabbedThumbnail.GetThumbnailPreview(trickyBorder);
            if (preview != null)
                TaskbarManager.Instance.TabbedThumbnail.RemoveThumbnailPreview(preview);
        }
    }
}